package com.sina.finance.api.framework.permission.timestamp;

/**
 * @File name : SimpleTimeStampChecker
 * @Description :
 * @Creator : cuiyong
 * @CreateTime : 15/12/1 14:07
 * @Version : 1.0
 * @Update records:
 * 1.0 15/12/1 by cuiyong: Created
 * All rights served : SINA Corporation
 */
public class SimpleTimeStampChecker implements TimeStampChecker {

    private int maxTimeSpan;

    public SimpleTimeStampChecker(int maxTimeSpan) {
        this.maxTimeSpan = maxTimeSpan;
    }

    @Override
    public boolean timeStampCheck(Long timeStamp) {

        boolean timeStampCheckOk = false;
        Long serverTimeStamp = System.currentTimeMillis();

        if (severTimeAfter(timeStamp, serverTimeStamp, Long.valueOf(maxTimeSpan))) {
            timeStampCheckOk = true;
        }
        return timeStampCheckOk;
    }

    private boolean severTimeAfter(Long timeStamp, Long serverTimeStamp, Long maxTimeSpan) {
        return serverTimeStamp - timeStamp >= 0 && serverTimeStamp - timeStamp < maxTimeSpan;
    }
}
